import dayjs from "dayjs";

export default function (
  rawData,
  tags,
  begTime,
  interval = 4,
  keys = ["airport", "type", "point"],
  fmt = "MM/DD HH:mm"
) {
  // let format = "MM/DD HH:mm";
  
  // Return an empty array while no valid params.
  if (!rawData || rawData == null || rawData.length == 0) return [];
  if (!tags || tags == null || tags.length == 0) return [];
  if (!keys || keys == null || keys.length == 0) return [];
  let output = [];
  let timestamp = dayjs(begTime).valueOf();
  // traverse data
  for (let i = 0; i < rawData.length; i++) {
    let moment = dayjs(timestamp).format(fmt);
    let cell = { moment, timestamp };
    // traverse tags
    for (let j = 0; j < tags.length; j++) {
      let keyValus = {
        airport: "OTHER",
        type: "OTHER",
        point: "OTHER",
      };
      let keyArray = tags[j].split("_");
      if (keyArray.length == 3) {
        keyValus.airport = keyArray[0];
        keyValus.type = keyArray[1];
        keyValus.point = keyArray[2];
      }

      // For airport data like ZUUU_OTHER
      // @2024-08-02
      if (keyArray.length == 2) {
        keyValus.airport = keyArray[0];
        keyValus.type = keyArray[1];
      }

      let value = rawData[i][j];
      // traverse keys
      if (keys.length >= 0) {
        // level-0
        if (!cell.total) {
          cell["total"] = value;
        } else {
          cell.total += value;
        }
      }
      if (keys.length >= 1) {
        if (!cell[keys[0]]) {
          // level-1 create category
          cell[keys[0]] = {};
        }
        if (!cell[keys[0]][keyValus[keys[0]]]) {
          // level-1 create key, value and total
          cell[keys[0]][keyValus[keys[0]]] = { total: value };
        } else {
          // level-1 cumulate total
          cell[keys[0]][keyValus[keys[0]]].total += value;
        }
      }
      if (keys.length >= 2) {
        if (!cell[keys[0]][keyValus[keys[0]]][keys[1]]) {
          // level-2 create
          cell[keys[0]][keyValus[keys[0]]][keys[1]] = {};
        }
        if (!cell[keys[0]][keyValus[keys[0]]][keys[1]][keyValus[keys[1]]]) {
          // level-2 create key, value and total
          cell[keys[0]][keyValus[keys[0]]][keys[1]][keyValus[keys[1]]] = {
            total: value,
          };
        } else {
          // level-2 cumulate total
          cell[keys[0]][keyValus[keys[0]]][keys[1]][keyValus[keys[1]]].total +=
            value;
        }
      }
      if (keys.length >= 3) {
        if (
          !cell[keys[0]][keyValus[keys[0]]][keys[1]][keyValus[keys[1]]][keys[2]]
        ) {
          // level-3 create
          cell[keys[0]][keyValus[keys[0]]][keys[1]][keyValus[keys[1]]][
            keys[2]
          ] = {};
        }
        // level-2 create key, value
        cell[keys[0]][keyValus[keys[0]]][keys[1]][keyValus[keys[1]]][keys[2]][
          keyValus[keys[2]]
        ] = value;
      }
    }
    output.push(cell);
    timestamp += 15 * 60000 * interval;
  }
  return output;
}
